<template>
  <auth-page>
    <view
      :style="{ '--themeColor': themeColor }"
      class="coupon"
      @click="toDetail"
    >
      <view class="left">
        <view
          class="price"
          v-if="
            couponContet.couponType == 'discount' ||
            couponContet.couponType == 'discount_wx_busi'
          "
        >
          <span>{{ couponContet.discountPercent / 10 }}</span
          >折
        </view>
        <view class="price" v-else>
          <span>{{ couponContet.denomination / 100 }}</span
          >元
        </view>
        <view class="condition">{{
          couponTypeOptions[couponContet.couponType]
        }}</view>
      </view>
      <view class="right">
        <view class="top">
          <view class="coupon-name">
            <view class="name">{{ couponContet.couponName }}</view>
            <!-- 我的券 -->
            <view v-if="couponContet.hasReceived">
              <view class="time" v-if="couponContet.day < 0">已过期</view>
              <view class="time pub-due" v-else-if="couponContet.day === 0"
                >今天即将过期</view
              >
              <view
                class="time pub-due"
                v-else-if="couponContet.day > 0 && couponContet.day < 8"
              >
                {{ couponContet.day }}天后过期 ({{
                  couponContet.dillExpireTime
                }}过期)
              </view>
              <view class="time pub-due" v-else-if="couponContet.effective > 0">
                {{ couponContet.effective }}天后 ({{
                  couponContet.effectiveTime
                }}可用)
              </view>
              <view class="time" v-else
                >{{ couponContet.dillExpireTime }} 过期</view
              >
            </view>
            <!-- 其他 -->
            <view v-else>
              <view class="time">{{ couponContet.expireTime }} 过期</view>
            </view>
          </view>
          <view
            class="pull"
            v-if="!hasReceived"
            @click="handleMixinAuth(getCoupon)"
            >领取</view
          >
          <view class="pull" v-else @click.stop="useCoupon()">使用</view>
        </view>
        <view class="bottom">
          <view class="shop-name">{{
            couponContet.pubDomainBusinessName
          }}</view>
          <view class="dince">{{
            $myTool.transitionDistance(couponContet.distance)
          }}</view>
        </view>
      </view>
    </view>
  </auth-page>
</template>

<script>
import { mapState } from "vuex";
import {
  receiveCoupon,
  getCouponJumpPubShopId,
} from "@/service/api/sp-coupon/index";
import { mixinAuth } from "@/mixin/sp-common/mixin-auth";
export default {
  mixins: [mixinAuth],
  props: {
    couponContet: {
      type: Object,
      default: {},
      require: true,
    },
    location: {
      type: Object,
      require: true,
      default: {
        longitude: "",
        latitude: "",
      },
    },
  },
  data() {
    return {
      couponTypeOptions: {
        exchange: "兑换券",
        cash: "代金券",
        cash_wx_busi: "代金券",
        cash_wx_pay: "代金券",
        discount: "折扣券",
        discount_wx_busi: "折扣券",
      },
      hasReceived: false,
    };
  },
  computed: {
    ...mapState("sp-common/base", ["themeColor"]),
  },
  created() {
    this.hasReceived = this.couponContet.hasReceived;
  },
  methods: {
    getCoupon() {
      let {
        couponContet: { receiveCode },
      } = this;
      receiveCoupon({ receiveCode })
        .then((res) => {
          if (res) {
            this.hasReceived = true;
            uni.showToast({
              title: "领取成功",
              icon: "success",
              duration: 2000,
            });
          }
        })
        .catch((res) => {
          wx.showToast({
            title: res,
            icon: "none",
          });
        });
    },
    useCoupon() {
      let {
        couponContet: { id: couponId },
        location,
      } = this;
      let queryParams = {
        couponId,
        ...location,
      };
      getCouponJumpPubShopId(queryParams).then((res) => {
        if (res == null) return;
        this.$myTool.jumpPage("shopIndex", { shopId: res });
      });
    },
    toDetail() {
      let {
        couponContet: { couponCode, hasReceived },
      } = this;
      if (!hasReceived) return;
      this.$myTool.jumpPage("couponDetail", { couponCode });
    },
  },
};
</script>
<style lang="scss" scoped>
.coupon {
  display: flex;
  background: #fff;
  border-radius: 15rpx;
  margin-bottom: 25rpx;
  .left {
    width: 200rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    font-size: 24rpx;
    font-weight: 500;
    color: #131313;
    position: relative;
    &::after {
      content: "";
      background: #f7f7f7;
      width: 30rpx;
      height: 30rpx;
      position: absolute;
      top: -15rpx;
      right: -15rpx;
      border-radius: 50%;
    }
    &::before {
      content: "";
      background: #f7f7f7;
      width: 30rpx;
      height: 30rpx;
      position: absolute;
      bottom: -15rpx;
      right: -15rpx;
      border-radius: 50%;
    }
    .price {
      font-size: 28rpx;
      span {
        font-size: 46rpx;
      }
    }
  }
  .right {
    flex: 1;
    padding: 30rpx 25rpx 25rpx;
    border-left: 1px dashed #efefef;
    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #ececec;
      padding-bottom: 30rpx;
      .coupon-name {
        color: #181818;
        .name {
          font-size: 29rpx;
          font-weight: bold;
        }
        .time {
          font-size: 24rpx;
          margin-top: 15rpx;
        }
        .pub-due {
          color: #e64340;
        }
      }
      .pull {
        width: 92rpx;
        height: 44rpx;
        background: var(--theme-color);
        border-radius: 22rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 22rpx;
        font-weight: 500;
        color: #ffffff;
      }
    }
    .bottom {
      margin-top: 25rpx;
      display: flex;
      justify-content: space-between;
      font-size: 24rpx;
      color: #131313;
      .dince {
        color: #888888;
      }
    }
  }
}
</style>
